package com.lanyou.cook.camel.service.impl;

import java.util.Date;
import java.util.UUID;

import org.apache.log4j.Logger;

import com.lanyou.cook.camel.commons.Constants;
import com.lanyou.cook.camel.dao.impl.AccessLogDao;
import com.lanyou.cook.camel.dao.impl.FtpDao;
import com.lanyou.cook.camel.datadictionary.IfEnableType;
import com.lanyou.cook.camel.datadictionary.TransportResultType;
import com.lanyou.cook.camel.entity.AccessLog;
import com.lanyou.cook.camel.entity.Ftp;
import com.lanyou.cook.camel.exception.AuthException;
import com.lanyou.cook.camel.service.FtpService;
import com.lanyou.cook.camel.util.CamelFactory;
import com.lanyou.cook.camel.util.CommUtil;

public class FtpServiceImpl implements FtpService{
	private static Logger logger = Logger.getLogger(FtpServiceImpl.class);
	private FtpDao ftpDao;
	
	private AccessLogDao accessLogDao;
		
	/*private static Map<String,Ftp> map = new HashMap<String, Ftp>();*/
	
	@Override
	public Ftp getFtpByCode(String code, String sourceIps, String targetIps, String fileName, String fileSize) throws AuthException {
		// TODO 自动生成的方法存根
		/*Ftp ftp = new Ftp();
		if(map.isEmpty()){
			System.out.println("ftpDao:" + ftpDao);
			ftp = ftpDao.findByCode(code);
			if(ftp == null){
				AccessLog accessLog = new AccessLog();
				accessLog.setId(UUID.randomUUID().toString());
				accessLog.setInterfaceId("");
				accessLog.setSourceIps("");
				accessLog.setTargetIps("");
				accessLog.setFileName("");
				accessLog.setFileSize("");
				accessLog.setTransportTime(new Date());
				accessLog.setIsEnable(IfEnableType.ENABLE.ValueCode);
				accessLog.setCreator("SYSTEM");
				accessLog.setModifier("SYSTEM");
				accessLog.setCreatedDate(new Date());
				accessLog.setLastUpdatedDate(new Date());
				accessLog.setTransportResult(TransportResultType.FAILD.ValueCode);
				accessLog.setTransportResutlDes(Constants.MSG_INTERFACE_NOTFOUND);
				accessLogDao.save(accessLog);
				throw new AuthException(Constants.MSG_INTERFACE_NOTFOUND, Constants.CODE_INTERFACE_NOTFOUND);
			}
			map.put(code, ftp);
		}else{
			ftp =  map.get(code);
		}
		return ftp;*/
		Ftp ftp =CamelFactory.ftpMap.get(code); 
		if (ftp == null) {
			logger.info("[FtpServiceImpl.getFtpByCode]:ftp为空 code="+code);
			AccessLog accessLog = new AccessLog();
			accessLog.setId(UUID.randomUUID().toString());
			accessLog.setInterfaceId("0");
			accessLog.setSourceIps(sourceIps);
			accessLog.setTargetIps(targetIps);
			accessLog.setFileName(fileName);
			accessLog.setFileSize(fileSize);
			accessLog.setTransportTime(new Date());
			accessLog.setIsEnable(IfEnableType.ENABLE.ValueCode);
			accessLog.setCreator("SYSTEM");
			accessLog.setModifier("SYSTEM");
			accessLog.setCreatedDate(new Date());
			accessLog.setLastUpdatedDate(new Date());
			accessLog.setTransportResult(TransportResultType.FAILD.ValueCode);
			accessLog.setTransportResutlDes(Constants.MSG_INTERFACE_NOTFOUND);
//			accessLog.setFieldSystemId(ftp.getSystemId());
			accessLog.setExecuteTaskNode(CommUtil.EXECUTE_TASK_NODE);
			accessLogDao.save(accessLog);
			throw new AuthException(Constants.MSG_INTERFACE_NOTFOUND,
					Constants.CODE_INTERFACE_NOTFOUND);
		}
		return ftp;
	}
	
	
	public FtpDao getFtpDao() {
		return ftpDao;
	}
	public void setFtpDao(FtpDao ftpDao) {
		this.ftpDao = ftpDao;
	}


	public AccessLogDao getAccessLogDao() {
		return accessLogDao;
	}


	public void setAccessLogDao(AccessLogDao accessLogDao) {
		this.accessLogDao = accessLogDao;
	}

}
